//Thread of the command window: parse an XML file created with glade and containing informations on the graphic interface

#include "com_main.h"

#define _PBSIGNAL_  //si problème de signal handler.

GtkWidget *window;



#ifdef _PBSIGNAL_
extern "C" {
#endif
    //remarque : le extern "C" et les G_MODULE_EXPORT devant chaque fonction ne sont pas obligatoires sous linux. Mais permet d'éviter erreur type "could not find signal handler blabla" sous windows
	#ifdef _PBSIGNAL_
        G_MODULE_EXPORT
        #endif
        void on_speed_value_changed(GtkWidget *object, par **val){
		(*val)->speed = (int)gtk_range_get_value(GTK_RANGE(object));
		printf("--%d--", (*val)->speed);
	}

	#ifdef _PBSIGNAL_
        G_MODULE_EXPORT
        #endif
        void on_bOuvrir_clicked(GtkObject *object, par **val){
		if(/*l_nbElt((*val)->liste_files)*/(*val)->liste_files.size() < NB_MAX_PROGS){ //if the number of loaded programs is too high, we don't do anything.
			//llist *pliste=&((*val)->liste_files);
			GtkWidget *p_dialog = NULL;
			p_dialog = gtk_file_chooser_dialog_new("Open a warrior source file", NULL,
				GTK_FILE_CHOOSER_ACTION_OPEN,
				GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
				GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
				NULL);

			if (gtk_dialog_run(GTK_DIALOG(p_dialog)) == GTK_RESPONSE_ACCEPT){
				//*pliste=l_addEnd(*pliste, (char*)gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(p_dialog)));
                                (*val)->liste_files.push_back((char*)gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(p_dialog)));
                                (*val)->full=true;
			}
			//l_print(*pliste);
                        for(unsigned int i=0; i<(*val)->liste_files.size(); i++) //affiche la liste
                            std::cout << (*val)->liste_files[i] << std::endl;
			gtk_widget_destroy(p_dialog);
		}
	}

	#ifdef _PBSIGNAL_
        G_MODULE_EXPORT
        #endif
        void on_bEffacer_clicked(GtkObject *object, par **val){
                (*val)->liste_files.clear();
		//llist *pliste=&((*val)->liste_files);
		//*pliste = l_delList(*pliste);
		//print_info("list of file emptied");
		(*val)->full=true;
	}

	#ifdef _PBSIGNAL_
        G_MODULE_EXPORT
        #endif
        void on_btRun_toggled(GtkObject *object, par **val){
		(*val)->play= ((*val)->play==true) ? false : true;
	}

	#ifdef _PBSIGNAL_
        G_MODULE_EXPORT
        #endif
        void on_bExit_clicked(GtkObject *object, gpointer user_data){ gtk_main_quit(); }
        
	#ifdef _PBSIGNAL_
        G_MODULE_EXPORT
        #endif
        void on_window_destroy(GtkObject *object, gpointer user_data){ gtk_main_quit(); }
#ifdef _PBSIGNAL_
}
#endif

void *com_main(void* val){
	GtkBuilder *builder;
	GtkWidget *speed;
	par *valeur=(par*)val;

	gtk_init(NULL, NULL); //initialization of GTK

	builder = gtk_builder_new();
	gtk_builder_add_from_file(builder, "interface.xml", NULL);//parse the XML file 'interface.xml' and add its contents to the builder object. 

	window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
	speed=GTK_WIDGET(gtk_builder_get_object(builder, "speed"));

	gtk_builder_connect_signals(builder, &valeur);          
	g_object_unref(G_OBJECT(builder));//we can free all the memory it used up with XML stuff

	valeur->speed = (int)gtk_range_get_value(GTK_RANGE(speed));	//set the initial speed

	gtk_widget_show(window);

        gtk_main();

    return 0;
}
